﻿/// <copyright file="AcessConcentrator.cs" company="not for commercial use">
/// Copyright (c) 6/4/2012 All Right Reserved
/// </copyright>
/// <author>Dimitry Kaplin</author>
/// <date>6/4/2012 21:49 </date>
/// <summary>Class containing some properties of AC</summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EthernetTools.Protocols.PPPoE;
using EthernetTools.Utils;

namespace EthernetTools
{
    public class AccessConcentrator: Object
    {
        #region Constant And Fields

        private long lastPADOTime = 0;
        private int PADOCount = 0;

        #endregion 
        
        #region Properties

        /// <summary>
        /// List of connected hosts
        /// </summary>
        public List<PPPoEConnectionInfo> Connected
        {
            get;
            private set;
        }

        /// <summary>
        /// 6 bytes - mac address
        /// </summary>
        public byte[] Mac
        {
            get;
            private set;
        }

        /// <summary>
        /// AC name
        /// </summary>
        public String AcName
        {
            get;
            private set;
        }

        /// <summary>
        /// Count PADT packets sended to me
        /// </summary>
        public int CountPadt2Me
        {
            get;
            set;
        }
        
        /// <summary>
        /// Pado speed packets/second, some times AC has buggy behavior, and sending A LOT OF PADO, 
        /// if speed will be more than in setting - the PADT packets will be sent to that ac
        /// </summary>
        public float PadoSpeed
        {
            get;
            private set;
        }

        /// <summary>
        /// How many times, AC behavior was buggy..
        /// </summary>
        public int SpeedLimitViolationTimes
        {
            get;
            set;
        }

        public PPPoEConnectionInfo this[byte[] hostMac]
        {
            get
            {
                foreach (PPPoEConnectionInfo pci in Connected)
                {
                    if (ByteUtils.Equals(pci.HostMac, hostMac))
                        return pci;
                }

                return null;
            }
        }
        #endregion
        
        #region Constructors And Destructors

        public AccessConcentrator(byte[] Mac, string acName = "")
        {
            this.Mac = Mac;
            this.AcName = acName;
            this.CountPadt2Me = 0;
            Connected = new List<PPPoEConnectionInfo>();
        }

        #endregion
        
        #region Public Methods

        public void Connect(byte[] mac, byte[] sessionId)
        {
            if (!Contains(mac, sessionId))
            {
                Connected.Add(new PPPoEConnectionInfo(this.Mac, mac, sessionId, DateTime.Now.Ticks));
            }
        }

        public void Remove(byte[] mac)
        {
            for (int i = 0; i < Connected.Count; i++)
            {
                if ( ByteUtils.Equals(Connected[i].HostMac,mac))
                {
                    Connected.RemoveAt(i);
                    --i;
                    continue;
                }
            }
        }

        public void Remove(byte[] mac, byte[] sessionId)
        {
            for (int i = 0; i < Connected.Count; i++)
            {
                if ( (ByteUtils.Equals(Connected[i].HostMac, mac) || ByteUtils.Equals(mac, Network.EthBroadCastMac))
                    && ByteUtils.Equals(Connected[i].SessionId, sessionId))
                {
                    Connected.RemoveAt(i);
                    --i;
                    continue;
                }
            }
        }

        public void PadoInc()
        {
            PADOCount++;

            if (DateTime.Now.Ticks - lastPADOTime > (10000000))
            {
                lastPADOTime = DateTime.Now.Ticks;
                PadoSpeed = 0;
                PADOCount = 0;
            }

            PadoSpeed = PADOCount;
        }

        public void PadoReset()
        {
            PadoSpeed = 0;
            PADOCount = 0;
        }
        #endregion

        #region Methods

        bool Contains(byte[] mac, byte[] sessionID)
        {
            foreach (PPPoEConnectionInfo pci in Connected)
            {
                if ( ByteUtils.Equals(pci.HostMac,mac))
                {
                    if (ByteUtils.Equals(pci.SessionId, sessionID))
                        return true;
                }
            }

            return false;
        }

        #endregion
    }
}
